package com.dayi.utils.pop;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dayi.R;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.zyyoona7.popup.EasyPopup;

import androidx.recyclerview.widget.RecyclerView;

public class GradeSubjSelectPopupUtils {


    private Activity activity;

    private EasyPopup selectPopup;

    private RecyclerView rvSelect;

    SelectAdapter selectAdapter;

    private String[] selectData;

    public GradeSubjSelectPopupUtils(Activity activity) {
        this.activity = activity;

        initSelect();
    }

    public void updateSelect(String[] selectArr) {

        selectData = selectArr;
        selectAdapter.notifyDataSetChanged();

    }


    private void initSelect() {

        selectPopup = EasyPopup.create()
                .setContext(activity)
                .setContentView(R.layout.popup_gradesubjselect)
                .setWidth(WindowManager.LayoutParams.MATCH_PARENT)
                .setHeight(WindowManager.LayoutParams.MATCH_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initWidget(view);

                    }

                })
                .setFocusAndOutsideEnable(true)
                .apply();

    }

    private void initWidget(View view) {

        rvSelect = view.findViewById(R.id.rv_select);

        selectAdapter = new SelectAdapter();
        MyLinearLayoutManager manager = new MyLinearLayoutManager(activity);
        rvSelect.setLayoutManager(manager);
        rvSelect.setAdapter(selectAdapter);

    }


    public void showSelectPopuPopu(View view) {

//        selectPopup.showAsDropDown(view, Gravity.CENTER, 0, 0);
        selectPopup.showAsDropDown(view);
    }

    public void dismiss() {
        selectPopup.dismiss();
    }


    /**
     * interface
     */
    private SelectItemPopupUtilsListener  listener;

    public void setSelectItemPopupUtilsListener(SelectItemPopupUtilsListener selectItemPopupUtilsListener) {

        this.listener = selectItemPopupUtilsListener;

    }

    public interface SelectItemPopupUtilsListener {

        void selectItem(String itemText);
    }


    class SelectAdapter extends RecyclerView.Adapter<SelectViewHolder> {

        @Override
        public SelectViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {

            View view = LayoutInflater.from(activity).inflate(R.layout.item_select, null);

            return new SelectViewHolder(view);
        }

        @Override
        public void onBindViewHolder(SelectViewHolder selectViewHolder, final int position) {

            selectViewHolder.tvText.setText(selectData[position]);
            selectViewHolder.linItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (listener != null) {
                        listener.selectItem(selectData[position]);
                    }

                }
            });

        }

        @Override
        public int getItemCount() {
            return selectData.length;
        }
    }


    class SelectViewHolder extends RecyclerView.ViewHolder {

        private TextView tvText;
        private LinearLayout linItem;

        public SelectViewHolder(View itemView) {
            super(itemView);

            tvText = itemView.findViewById(R.id.tv_text);
            linItem = itemView.findViewById(R.id.lin_item);

        }
    }
}
